package org.feng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.feng.entity.SysDictData;
import org.feng.service.ISysDictDataService;
import org.feng.condition.Condition;
import org.feng.annotation.AccessLog;
import org.feng.base.entity.Result;
import org.springframework.web.bind.annotation.*;
import org.feng.entity.SysDict;
import org.feng.service.ISysDictService;
import org.feng.base.controller.BaseController;

import jakarta.annotation.Resource;

import java.util.List;
import java.util.Arrays;

/**
 * 字典表
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@RestController
@RequestMapping("/system/sysDict")
@Tag(name = "字典")
public class SysDictController extends BaseController {

    @Resource
    private ISysDictService sysDictService;
    @Resource
    private ISysDictDataService sysDictDataService;

    /**
     * 获取字典
     *
     * @param dictData 字段类型值
     * @return 返回排序字典数据
     */
    @GetMapping("/{dictData}")
    @Operation(summary = "获取字典")
    public Result<List<SysDictData>> getDictData(@PathVariable String dictData) {
        SysDict sysDict = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery().eq(SysDict::getCode, dictData));
        if (sysDict == null) {
            return Result.success(List.of());
        } else {
            List<SysDictData> list = sysDictDataService.list(Wrappers.<SysDictData>lambdaQuery()
                    .eq(SysDictData::getDictId, sysDict.getId())
                    .eq(SysDictData::getStatus, true)
                    .orderByAsc(SysDictData::getSort));
            return Result.success(list);
        }
    }

    /**
     * 分页列表
     *
     * @param sysDict 查询体
     * @return 返回体
     */
    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public Result<IPage<SysDict>> pageList(SysDict sysDict) {
        return Result.success(sysDictService.page(Condition.getPage(), Wrappers.lambdaQuery(sysDict)));
    }

    /**
     * 列表
     *
     * @param sysDict 查询体
     * @return 返回体
     */
    @GetMapping("/list")
    @Operation(summary = "列表")
    public Result<List<SysDict>> list(SysDict sysDict) {
        return Result.success(sysDictService.list(Wrappers.lambdaQuery(sysDict)));
    }

    /**
     * 保存
     *
     * @param sysDict 保存体
     * @return 返回体
     */
    @PostMapping("/save")
    @AccessLog("保存字典表")
    @Operation(summary = "保存")
    public Result<Boolean> save(@RequestBody SysDict sysDict) {
        return Result.status(sysDictService.save(sysDict));
    }

    /**
     * 更新
     *
     * @param sysDict 更新体
     * @return 返回体
     */
    @PostMapping("/update")
    @AccessLog("更新字典表")
    @Operation(summary = "更新")
    public Result<Boolean> update(@RequestBody SysDict sysDict) {
        return Result.status(sysDictService.updateById(sysDict));
    }

    /**
     * 删除
     *
     * @param ids 逗号分割的id
     * @return 返回体
     */
    @GetMapping("/remove")
    @AccessLog("删除字典表")
    @Operation(summary = "删除")
    public Result<Boolean> remove(@RequestParam("ids") String ids) {
        return Result.status(sysDictService.removeBatchByIds(Arrays.asList(ids.split(","))));
    }
}
